package cn.itsource.core;

import cn.itsource.config.CosProperties;
import com.qcloud.cos.model.ObjectMetadata;
import com.qcloud.cos.model.PutObjectRequest;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.io.File;
import java.io.InputStream;
import java.util.UUID;

@Data
@AllArgsConstructor
public class CosTemplate {

    private CosProperties cosProperties;

    private GetCosClient cosClient;

    public String CosUpload(InputStream inputStream, String suffix, ObjectMetadata metadata){
        UUID uuid = UUID.randomUUID();
        String keyName =cosProperties.getKey()+"/"+uuid+suffix;
        PutObjectRequest putObjectRequest = new PutObjectRequest(cosProperties.getBucketName(),keyName,inputStream,metadata);
        cosClient.getCosClient(cosProperties).putObject(putObjectRequest);
        String url = String.format(cosProperties.getUrl(), cosProperties.getBucketName(), cosProperties.getRegionName(), keyName);
        //https://fccar-0228-zls-1327930848.cos.ap-chongqing.myqcloud.com/Images/9c331ea0-24ac-4058-a6f6-2d607b39fb62.jpg
        return url;
    }

}
